<?php

/*
 * @copyright :     Ienter asia Technologies
 * @author :        Quyet
 * @version         v.1.0
 * @created         30/6/2014
 * @Description :   Controller shipinfo
 */
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Mod_shiftinfo extends MYAD_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('Mmod_shiftinfo', 'si');
    }

    function index($page = 0) {
        $data = $this->si->getAllData($page);
        $this->load->view('layout', $data);
    }

    function updateShipInfo() {
        $shiftId = (integer) $this->input->post('txtShiftId');
        $shipInfo = TRUE;
        if ($shiftId) {
            $shipInfo = $this->si->countShipInfoByID($shiftId);
        }
        if (!$shipInfo) {
            redirect('/shift-info');
        }
        $temp = array();
        if (isset($_POST['btnSubmit'])) {
            $validateFlag = $this->si->validate();
            if ($validateFlag) {
                /* validate success */
                $temp = $this->si->updateShipInfo($shiftId);
            }
        }
        $data = $this->si->getAddShipInfo($shiftId);
        $this->load->view('layout', array_merge($data, $temp));
    }

    /*
     * @author :        Quyet
     * @created:        30/6/2014
     * @Description:    validate for total work hour
     * @Input :         Null
     * @Output :        TRUE/FASLE
     */

    function validate_totalWorkHour() {
        /* check logic */
        $txtStartTime = strtotime($_POST['txtStartTime']);
        $txtEndTime = strtotime($_POST['txtEndTime']);
        $txtStartRestTime1 = strtotime($_POST['txtStartRestTime1']);
        $txtEndRestTime1 = strtotime($_POST['txtEndRestTime1']);
        $txtTotalWorkHours = $_POST['txtTotalWorkHours'];
        $temp = $txtEndTime - $txtEndRestTime1;
        $temp += $txtStartRestTime1 - $txtStartTime;
        $temp = $temp / 3600;
        if ($temp != $txtTotalWorkHours) {
            $this->form_validation->set_message('validate_totalWorkHour', '%s ' . $this->lang->line('Sif_validate_totalWorkHour'));
            return FALSE;
        } else {
            return TRUE;
        }
    }

    /*
     * @author :        Quyet
     * @created:        18/8/2014
     * @Description:    validation for Restime 2
     * @Input :         Null
     * @Output :        TRUE/FASLE
     */

    function validate_restime2() {
        $txtEndTime = strtotime($_POST['txtEndTime']);
        $txtRestTime2 = $_POST['txtRestTime2'] * 60;
        $txtEndEveningOvertime = strtotime($_POST['txtEndEveningOvertime']);

        if ($txtRestTime2 >= ($txtEndEveningOvertime - $txtEndTime)) {
            $this->form_validation->set_message('validate_restime2', '%s ' . $this->lang->line('Sif_validate_restime2'));
            return FALSE;
        } else {
            return TRUE;
        }
    }

    /*
     * @author :        Quyet
     * @created:        18/8/2014
     * @Description:    validation for Restime 3
     * @Input :         Null
     * @Output :        TRUE/FASLE
     */

    function validate_restime3() {
        /* check logic */
        $txtEndEveningOvertime = strtotime($_POST['txtEndEveningOvertime']);
        $txtRestTime3 = $_POST['txtRestTime3'] * 60;
        $txtStartTime = strtotime($_POST['txtStartTime']) + 86400;
        if ($txtRestTime3 >= $txtStartTime - $txtEndEveningOvertime) {
            $this->form_validation->set_message('validate_restime3', '%s ' . $this->lang->line('Sif_validate_restime3'));
            return FALSE;
        } else {
            return TRUE;
        }
    }

}

?>